﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Domain;

namespace SharedGenomics.Workbench.Presenters
{
    public class UcscBrowserPresenter : Presenter
    {
        public UcscBrowserPresenter(IXhtmlView view)
            : base(view)
        {
            this.XhtmlView = view;
        }

        public IList<WiggleElement> WiggleElements
        {
            //NOTE: this code was ripped straight out of the AnalysisControl code-behind from the 'old' version of the workbench 
            set
            {
                IList<WiggleElement> elements = value;

                if (elements == null || elements.Count == 0)
                    return;

                int chromosome = elements[0].Chromosome;
                long lowerBound = elements[0].LowerBound;
                long upperBound = elements[0].UpperBound;
                foreach (WiggleElement element in elements.Skip(1))
                {
                    if (chromosome == element.Chromosome)
                    {
                        if (element.LowerBound < lowerBound)
                            lowerBound = element.LowerBound;
                        if (element.UpperBound > upperBound)
                            upperBound = element.UpperBound;
                    }
                }
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
                builder.AppendLine("<html>");
                builder.AppendLine("<head>");
                builder.AppendLine("<title>A form</title>");
                builder.AppendLine("</head>");
                builder.AppendLine("<body onload=\"document.mainform.submit()\">");
                builder.AppendLine("Submitting track data to UCSC...");
                builder.AppendLine("<form id=\"mainform\" action=\"http://genome.ucsc.edu/cgi-bin/hgCustom\" method=\"POST\"  enctype=\"multipart/form-data\" name=\"mainForm\">");
                builder.AppendLine("<input type=\"hidden\" name=\"clade\" value=\"vertebrate\" />");
                builder.AppendLine("<input type=\"hidden\" name=\"org\" value=\"Human\" />");
                builder.AppendLine("<input type=\"hidden\" name=\"db\" value=\"hg18\" />");
                builder.AppendLine("<input type=\"hidden\" name=\"Submit\" value=\"Submit\" />");
                builder.AppendLine("<textarea name=\"hgct_customText\" rows=\"7\" cols=\"73\" style=\"display: none\">");
                builder.AppendLine(string.Format("browser position chr{0}:{1}-{2}", chromosome, lowerBound, upperBound));
                // builder.AppendLine("browser hide all");
                builder.AppendLine("browser pack refGene encodeRegions");
                builder.AppendLine("browser full altGraph");
                builder.AppendLine("track type=wiggle_0 name=\"-log(P)\" description=\"-log10(P) of selected rows\" visibility=full color=0,0,0 priority=20");
                foreach (WiggleElement element in elements)
                {
                    if (chromosome == element.Chromosome)
                    {
                        builder.AppendLine(string.Format("chr{0}\t{1}\t{2}\t{3}", element.Chromosome, element.LowerBound, element.UpperBound, element.Value));
                    }
                }
                builder.AppendLine("</textarea>");
                builder.AppendLine("</form>");
                builder.AppendLine("</body>");
                builder.AppendLine("</html>");

                this.XhtmlView.Xhtml = builder.ToString();
            }
        }

        public override bool SnapshotOnOpen
        {
            get { return true; }
        }

        private IXhtmlView XhtmlView { get; set; }
    }
}
